<template>
  <drag-modal
    title="新增"
    :width="450"
    :visible="visible"
    :destroyOnClose="true"
    :confirmLoading="submitLoading"
    :maskClosable="false"
    @ok="ok"
    @cancel="cancel"
  >
    <a-spin :spinning="loading">
      <a-form-model :model="form" ref="form" :rules="validatorRules" :label-col="{span: 5}" :wrapperCol="{span: 16, offset: 1}">
        <div class="fold">
          <a-form-model-item
            label="分组名称"
            prop="name"
          >
            <a-input allowClear v-model="form.name" />
          </a-form-model-item>
          <a-form-model-item
            label="分组编码"
            prop="code"
          >
            <a-input allowClear v-model="form.code" />
          </a-form-model-item>
          <a-form-model-item
            label="备注"
            prop="remarks"
          >
            <a-textarea allowClear v-model="form.remarks" :auto-size="{ minRows: 3, maxRows: 3 }" />
          </a-form-model-item>
        </div>
      </a-form-model>
    </a-spin>
  </drag-modal>
</template>

<script>
import {dictGroupService} from '@/services'

export default {
  name: 'AddModal',
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      loading: false,
      submitLoading: false,
      form: {
        name: undefined,
        code: undefined,
        remarks: undefined
      },
      validatorRules: {
        name: [
          { required: true, message: '请输入分组名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入分组编码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.$emit('cancel')
    },
    ok() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submitLoading = true
          dictGroupService.add(JSON.stringify(this.form)).then(res => {
            const result = res.data
            if (result.code === 200) {
              this.$emit('ok')
              this.$message.success(result.msg)
            }
          }).finally(this.submitLoading = false)
        }
      })
    }
  }
}
</script>